// pages/house/RentalHouseDetail/composite/composite.js
import api from "../../../../common/api";
import { IMG_COMPOUND_VIDEO } from "../../../../common/menuId";
import p from "../../../../utils/platform";
import util from "../../../../utils/util"
import { httpUrlCheck } from "../../../../utils/utilsNew";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
    ],
    url: util.ImagesUrl,
    keys: [], //一选择站点的id
    type: 'video',
  },

    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let delType = options.delType;
    if(options.type=='photo'){
      let delId = options.delId;
      let menuId = options.menuId;
      let type = options.type;
      this.setData({
        delId,
        menuId,
        type,
        delType
      })
      this.getVrDataByColumn();
    } else if (options.type=='video'){
      wx.setNavigationBarTitle({
        title: '选择合成视频图片',
      })
      let delId = options.delId;
      let type = options.type;
      this.setData({
        delId,
        type,
        delType
      })
      this.getVrDataByColumn();
    }
  },

  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    const values = e.detail.value
    if(this.data.type=='video'){
      if(values.length>9){
        values.pop();
        wx.showModal({
          content: '您当前选择了'+values.length+'张图片，最大选择数量为9张，请重新选择～',
          showCancel: false,
          success (res) {
            if(res.confirm){
            }
          }
        })
      }
    }
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].name === values[j]) {
          items[i].checked = true
          break
        }
      }
    }


    this.setData({
      items,
      keys: e.detail.value
    })
  },
  // 获得vr点位图片
  getVrDataByColumn(){
      if(this.data.type == 'photo') {
        api.getVrDataByColumn({
          delId: this.data.delId,
          type: "0"
        }, res => {
          this.setData({
            items: res.data.content.notSyncPanoramaScenes.panorama.map((item) => {
              return {
                ...item,
                thumburl: httpUrlCheck(item.thumburl) ? item.thumburl : this.data.url + item.thumburl + '/188_160_t.jpg'
              }
            })
          })
        })

        // 更换为图片合成视频了
      } else if (this.data.type == 'video') {
        api.getHouseInfo({
          houDelId: this.data.delId,
        }, ({data}) => {
          const { isSuccess, content } = data
          if (!isSuccess) return   
          const items = content.houImageVO
            .filter((item) => {
              return item.tagCode != 'huxingtu'
            }).map((item) => {
              return {
                name: item.fileId,
                thumburl: item.url,
                title: item.tagCode ? item.tagName : ''
              }
            })

          this.setData({items})
        })
      }
  },
  // 提交全景图任务
  submitVrSphereTask(){
    api.submitVrSphereTaskMq({
    // api.submitVrSphereTask({
      deiId: this.data.delId,
      sceneIdList: this.data.keys,
    }, res => {
      api.getMemberPower({menuId: this.data.menuId},(res) => {
        if(res.data.isSuccess){
          wx.setStorageSync('memberInfo', res.data.content.rows)
        }
      })
      
      wx.showModal({
        content: '全景图合成任务已提交，大约需1分钟，在首页可查看进度～',
        showCancel: false,
        success (res) {
          if(res.confirm){
            wx.navigateBack();
          }
        }
      })
    })
  },
  // 合成视频
  compositeVideo(){
    if(this.data.keys.length<5){
      wx.showToast({
        title: '您当前选择了'+this.data.keys.length+'张图片，最低选择数量为5张，请重新选择～',
        icon: 'none'
      })
      return
    } else {
      // 不跳转配音界面直接发送视频合成任务
      api.submitImgsToVideoTaskMq({
        delId: this.data.delId,
        dubbed: '2',
        fileIdList: this.data.keys
      }, ({ data }) => {
        const { isSuccess, content } = data
        if (!isSuccess) return

          p.showModal({
            content: '视频合成任务已提交，大概需2分钟，在房源列表页可查看进度',
            showCancel: false,
            confirmText: "我知道了"
          }).then(() => {
            api.getMemberPower({menuId: IMG_COMPOUND_VIDEO},(res) => {
              if(res.data.isSuccess){
                wx.setStorageSync('memberInfo', res.data.content.rows)
              }
            })
            wx.navigateBack()
          })
      })

      // wx.navigateTo({
      //   url: '/pages/house/RentalHouseDetail/addAudio/addAudio?required=0&delId='+ this.data.delId + '&sceneIdList=' + this.data.keys + '&delType='+ this.data.delType,
      // })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    if (prevPage && prevPage.setData) {
      prevPage.setData({
        isUpdate: true
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})